import axios from "axios";
import runtime_config from "./config";
import message from "./message.js";
import store from '../common/store.js';
import common from '../common/common.js';

axios.defaults.withCredentials = true;
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
axios.defaults.headers.put["Content-Type"] = "application/x-www-form-urlencoded";
// axios.defaults.headers.put["Content-Type"] = "application/json";
console.log(axios.defaults.headers);


console.log(process.env.NODE_ENV);
let _instance = axios.create({
	withCredentials: true,
	baseURL: runtime_config[process.env.NODE_ENV].path,
});
// _instance.interceptors.request.use(
// 	config => {
// 		return config;
// 	},
// 	error => {
// 		console.log("出错了")
// 	}
// );



_instance.interceptors.request.use(
    config => {
		
		let TOKEN = common.getAttribute("TOKEN");
		
		if (TOKEN) {
			config.headers.token = TOKEN;
		}

        return config;
    },
    error => {
        console.log(error);
        return Promise.reject(error);
    });

const httpObject = {

	_changeParam: param => {
		if (process.env.NODE_ENV !== "local") {
			param.params = param.param;
		}
		return param;
	},
	uploadFile: function(files) {

		return new Promise((resolve, reject) => {
			let config = {
				headers: {
					"Content-Type": "multipart/form-data"
				}
			};
			
			let param = new FormData();
			
			files.forEach(file => {
				param.append("files", file);
			});

			_instance.post("/system/upload/file", param, config).then(res => {
				let response = typeof res === "string" ? JSON.parse(res) : res;
				let responseData = response.data;
				let status = responseData.STATU;
				if ("SUCCESS" === status) {
					resolve(responseData.DATA);
				} else if (status === "NOT_LOGIN") {
					store.commit('loginOut');
				} else if (status === "FAIL") {
					message.warning(responseData.MESSAGE);
					reject(responseData.MESSAGE);
				} else if (status === "ERROR") {
					message.error(responseData.MESSAGE);
					reject(responseData.MESSAGE);
				} else {
					message.error("系统出现未知错误！");
					reject("系统出现未知错误！");
				}
			})
			.catch(err => {
				message.error("系统出现未知错误！");
				reject("系统出现未知错误！");
			});
		});

	},
	put: param => {
		param.method = "put";
		return httpObject._request(httpObject._changeParam(param));
	},
	get: param => {
		param.method = "get";
		return httpObject._request(httpObject._changeParam(param));
	},
	post: param => {
		param.method = "post";
		return httpObject._request(httpObject._changeParam(param));
	},
	"delete": param => {
		param.method = "delete";
		return httpObject._request(httpObject._changeParam(param));
	},
	_request: param => {
		return new Promise((resolve, reject) => {
			
			
			_instance.request(param)
				.then(res => {
					
					if (res.headers.token) {
						console.log(res.headers.token);
						common.setAttribute("TOKEN", res.headers.token);
					}
					let response = typeof res === "string" ? JSON.parse(res) : res;
					let responseData = response.data;
					let status = responseData.STATU;

					if ("SUCCESS" === status) {
						resolve(responseData.DATA);
					} else if (status === "NOT_LOGIN") {
						store.commit('loginOut');
					} else if (status === "FAIL") {
						message.warning(responseData.MESSAGE);
					} else if (status === "ERROR") {
						message.error(responseData.MESSAGE);
					} else {
						message.error("系统出现未知错误！");
					}
				})
				.catch(err => {
					console.log(err);
					message.error("系统出现未知错误！");
				});
		});
	}
}



export default httpObject;
